﻿package com.edward.service;


import java.text.SimpleDateFormat;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.edward.PagerModel;
import com.edward.model.InvoiceList;


public class InvoiceListManager extends HibernateDaoSupport {

	public void addInvoiceList(InvoiceList invoiceList) {
		
		getHibernateTemplate().save(invoiceList);
	}
	
	public void updateInvoiceList(InvoiceList invoiceList) {
		
		getHibernateTemplate().update(invoiceList);
	}
	
	public void deleteInvoiceList(int id){
		
		getHibernateTemplate().delete(getInvoiceList(id));
	}
	
	public InvoiceList getInvoiceList(int id){
		InvoiceList d =(InvoiceList)getHibernateTemplate().get(InvoiceList.class, id);
		return d;
	}
	

	@SuppressWarnings("unchecked")
	public List<InvoiceList> listInvoiceList() {
		return getHibernateTemplate().find("from InvoiceList");
	}

	
	@SuppressWarnings("unchecked")	
	public PagerModel pagerList(int start,int limit) {
		List <InvoiceList>list = getHibernateTemplate().find("from InvoiceList" );
		int total = list.size();
		int toIndex = start + limit;
		if(toIndex>total){
			toIndex =  total;
		}
		List <InvoiceList>datas = list.subList(start, toIndex);
		PagerModel pm = new PagerModel();
		pm.setTotal(total);
		pm.setDatas(datas);
		return pm;
		
	}

	
	@SuppressWarnings("unchecked")	
	public PagerModel pagerList(int start,int limit,InvoiceList invoiceList) {
		List <InvoiceList>list;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String sql = "from InvoiceList i where ";;
		int count = 0;
		if(!invoiceList.getCompany().equals("")){
			sql += "i.company = '" + invoiceList.getCompany() + "'";
			count++;
		}
		if(!invoiceList.getInvoiceNum().equals("")){
			if (count > 0) {
				sql += " and ";
			}
			sql += "i.invoiceNum = '" + invoiceList.getInvoiceNum() + "'";
			count++;
		}
		if(!invoiceList.getPayment().equals("")){
			if (count > 0) {
				sql += " and ";
			}
			sql += "i.payment like '" + invoiceList.getPayment() + "%'";
			count++;
		}
		if(invoiceList.getDate() != null && invoiceList.getEndDate() == null ){
			String d = sdf.format(invoiceList.getDate());
			if (count > 0) {
				sql += " and ";
			}
			sql += "i.date >= '" + d + "'";
		}
		if(invoiceList.getEndDate() != null && invoiceList.getDate() == null ){
			String d = sdf.format(invoiceList.getEndDate());
			if (count > 0) {
				sql += " and ";
			}
			sql += "i.date <= '" + d + "'";
		}
		if(invoiceList.getEndDate() != null && invoiceList.getDate() != null ){
			String d1 = sdf.format(invoiceList.getDate());
			String d2 = sdf.format(invoiceList.getEndDate());
			if (count > 0) {
				sql += " and ";
			}
			sql += "i.date between '" + d1 + "' and '" +d2  +"'";
		}
		list = getHibernateTemplate().find(sql);
		int total = list.size();
		int toIndex = start + limit;
		if(toIndex>total){
			toIndex =  total;
		}
		List <InvoiceList>datas = list.subList(start, toIndex);
		PagerModel pm = new PagerModel();
		pm.setTotal(total);
		pm.setDatas(datas);
		return pm;
		
	}
	
	//通过点击客户查询订单列表
	@SuppressWarnings("unchecked")	
	public PagerModel pagerList(int start,int limit,String query) {
		List <InvoiceList>list = getHibernateTemplate().find("from InvoiceList i where i.company = '" + query + "'" );
		int total = list.size();
		int toIndex = start + limit;
		if(toIndex>total){
			toIndex =  total;
		}
		List <InvoiceList>datas = list.subList(start, toIndex);
		PagerModel pm = new PagerModel();
		pm.setTotal(total);
		pm.setDatas(datas);
		return pm;
		
	}

	//自动添加对应表格的单号
	@SuppressWarnings("unchecked")	
	public String listNum(){
		List <InvoiceList>list = getHibernateTemplate().find("from InvoiceList");
		int i = list.size();
		if(i==0){
			return "000001";
		}else{
			String tmp = list.get(i-1).getInvoiceNum();
			while(tmp.indexOf(0) == '0'){
				tmp = tmp.substring(1);
			}
			int t = Integer.parseInt(tmp) + 1;
			tmp = t + "";
			for(int j=6;j>tmp.length();){
				tmp ="0"+tmp;
			}
			return tmp;
		}
		
		
	}

}



